package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.item.dto.CategoryDTO;
import com.leyou.item.entity.Category;
import com.leyou.item.entity.CategoryBrand;
import com.leyou.item.mapper.CategoryMapper;
import com.leyou.item.service.CategoryBrandService;
import com.leyou.item.service.CategoryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Resource
    private CategoryBrandService categoryBrandService;
    @Override
    public List<CategoryDTO> queryCategoryByBrandId(Long brandId) {
        List<CategoryBrand> categoryBrands = categoryBrandService.query().eq("brand_id", brandId).list();
        if (CollectionUtils.isEmpty(categoryBrands)){
            return Collections.emptyList();
        }
        List<Long> cateIds = new ArrayList<>();
        categoryBrands.forEach(categoryBrand -> cateIds.add(categoryBrand.getCategoryId()));
        List<Category> categories = listByIds(cateIds);
        return CategoryDTO.convertEntityList(categories);
    }
}
